package net.kesumu.general.android.opengl.object;

import static net.kesumu.general.android.opengl.object.YushabuMessageConstants.*;
import static net.kesumu.general.android.opengl.util.Constants.BYTES_PER_FLOAT;

import java.util.List;

import android.util.FloatMath;
import net.kesumu.general.android.opengl.object.Geometry.Circle;
import net.kesumu.general.android.opengl.object.Geometry.Point;
import net.kesumu.general.android.opengl.object.Geometry.RingSector;
import net.kesumu.general.android.opengl.object.Geometry.Sector;
import net.kesumu.general.android.opengl.object.ObjectBuilder.DrawCommand;
import net.kesumu.general.android.opengl.object.ObjectBuilder.GeneratedData;
import net.kesumu.general.android.opengl.program.ColorShaderProgram;
import net.kesumu.general.android.opengl.util.VertexArray;

public class YushabuPhotoBackgound {
    
    private static final int POSITION_COMPONENT_COUNT = 3;
    private static final int COLOR_COMPONENT_COUNT = 3;
    private static final int STRIDE = 
            (POSITION_COMPONENT_COUNT + COLOR_COMPONENT_COUNT) * BYTES_PER_FLOAT;
    
    public static final int INITIATED = 0,
                            FOCUSING = 1,
                            FOCUSED = 2,
                            UNFOCUSING = 3,
                            UNFOCUSED = 4;
    
    public final float focusedScaleFactor = (1f*PHOTO_FOCUSED_FG_WIDTH)/(1f*PHOTO_WIDTH);
    public final float focusedScaleFactorDelt = 0.1f*(focusedScaleFactor - 1.0f);
    public float focusingScaleFactor = 1f;
    
    public final int color;
    public int state;
    public Point circleCenter;
    
    private final VertexArray vertexArray;
    private final List<DrawCommand> drawList;
    
    private final VertexArray focusedVertexArray;
    private final List<DrawCommand> focusedDrawList;

    public YushabuPhotoBackgound(Point center, int color, int numPoints) {
        this.state = INITIATED;
        this.color = color;
        
        circleCenter = center;
        
        Point circleCenter1 = new Point(circleCenter.x - PHOTO_DELT, circleCenter.y, circleCenter.z);
        Point circleCenter2 = new Point(circleCenter.x + PHOTO_DELT, circleCenter.y, circleCenter.z);
        
        Circle bgCircle1 = new Circle(circleCenter1, PHOTO_WIDTH/2f);
        Circle bgCircle2 = new Circle(circleCenter2, PHOTO_WIDTH/2f);
        Circle fgCircle = new Circle(circleCenter, PHOTO_WIDTH/2f);
        
        GeneratedData generatedData = ObjectBuilder.createYushabuPhotoBackground(bgCircle1, bgCircle2, fgCircle, color, numPoints);
        
        vertexArray = new VertexArray(generatedData.vertexData);
        drawList = generatedData.drawList;
        
        Circle focusedBgCircle = new Circle(circleCenter, PHOTO_FOCUSED_BG_WIDTH/2f);
        Circle focusedFgCircle = new Circle(circleCenter, PHOTO_FOCUSED_FG_WIDTH/2f);
        
        GeneratedData focusedGeneratedData = ObjectBuilder.createFocusedYushabuPhotoBackground(focusedBgCircle, focusedFgCircle, color, numPoints);
        
        focusedVertexArray = new VertexArray(focusedGeneratedData.vertexData);
        focusedDrawList = focusedGeneratedData.drawList;
    }
    
    public void bindData(ColorShaderProgram colorProgram) {
        vertexArray.setVertexAttribPointer(0,
            colorProgram.getPositionAttributeLocation(),
            POSITION_COMPONENT_COUNT, STRIDE);
        vertexArray.setVertexAttribPointer(POSITION_COMPONENT_COUNT,
                colorProgram.getColorAttributeLocation(),
                COLOR_COMPONENT_COUNT, STRIDE);
    }
    
    public void focusedBindData(ColorShaderProgram colorProgram) {
        focusedVertexArray.setVertexAttribPointer(0,
            colorProgram.getPositionAttributeLocation(),
            POSITION_COMPONENT_COUNT, STRIDE);
        focusedVertexArray.setVertexAttribPointer(POSITION_COMPONENT_COUNT,
                colorProgram.getColorAttributeLocation(),
                COLOR_COMPONENT_COUNT, STRIDE);
    }
    
    public void draw() {
        for (DrawCommand drawCommand : drawList) {
            drawCommand.draw();
        }
    }
    
    public void focusedDraw() {
        for (DrawCommand drawCommand : focusedDrawList) {
            drawCommand.draw();
        }
    }

}
